package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.itemLoadingRequest;
import com.dc.gameserver.proto.itemserverprotoVo.itemLoadingResponse;
import com.dc.gameserver.proto.itemserverprotoVo.itemVO;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleItemProps;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-27 </br>
 *         Time:下午2:34 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：道具加载请求
 */
@Service
public class itemLoadingController extends AbstractController {

    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= itemLoadingRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=itemLoadingRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        itemLoadingRequest loadingRequest= (itemLoadingRequest) messageLite;
        int type=loadingRequest.getType();
        loadingRequest=null;
        FastTable<TRoleItemProps> itemList=playerService.itemLoading(player.getID(),type);
        itemLoadingResponse.Builder builder=itemLoadingResponse.newBuilder();
        if (itemList!=null){
            for (TRoleItemProps item:itemList){
                itemVO.Builder vo=itemVO.newBuilder();
                vo.setItemID(item.getPropsid());//道具编号 来自数据库自增长
                vo.setItemTypeID(item.getPropstypeid());
                vo.setItemNum(item.getPropsnum());
                vo.setItemTimeOut(item.getPropstimeout());
                builder.addItemList(vo.build());
            }
            itemList.clear();
            itemList=null;
        }
        builder.setType(type);
        player.DirectSend(wrappedBufferShort(itemLoadingResponse.msgID.ID_VALUE,builder.build()));
    }
}
